/**
 * Copyright ©2003~2019年3月28日 szkingdom
 * @author raohj
 * @date 2019年3月28日
 * @file RFileWriter.java
 * @comment : please input your comment to this file
 * @version : input version
 * @modify:
 * 		1、 2019年3月28日 由 raohj 新建文件
 */
package com.ccnu.demojava.fileio;

import com.ccnu.rc.date.RDate;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.WritableByteChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Date;
import java.util.EnumSet;
import java.util.Random;

/**
 * @author raohj
 * @date 2019年3月28日
 * @version 
 * @comment to add your comment
 */
public class RFileWriter {
	private static long GB_1 = 1024*1024*1024;
	private static long WRITE_SIZE = GB_1/1024;
	private static String content = new String("成功是失败之母\n");
	private static Random rd = new Random(new Date().getTime());

	/**
	 * @author raohj
	 * @date 2019年3月28日
	 * @comment 使用outputStream写入文件
	 * @param 
	 * @return 
	 */
	public static void writeByOutputStream() {
		try {
			Integer d0 = rd.nextInt();
			Path createF = Paths.get("D:", "temp", "tmp", d0.toString());
			// 创建文件
			OutputStream os = Files.newOutputStream(createF, StandardOpenOption.CREATE);
			long start = RDate.getNanoTime();
			for(int i=0; i<WRITE_SIZE; i+=content.getBytes().length) {
				os.write(content.getBytes());
			}
			os.flush();	
			os.close();
			long end = RDate.getNanoTime();
			System.out.println("outputStream写入文件" + createF.getFileName() +"总耗时:" + (end - start) + "(纳秒)");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @author raohj
	 * @date 2019年3月28日
	 * @comment 使用outputStream写入文件
	 * @param 
	 * @return 
	 */
	public static void writeByOutputStreamOptimization() {
		try {
			Integer d0 = rd.nextInt();
			Path createF = Paths.get("D:", "temp", "tmp", d0.toString());
			// 创建文件
			OutputStream os = Files.newOutputStream(createF, StandardOpenOption.CREATE);
			ByteBuffer byteContent = ByteBuffer.allocate(10240);
			long start = RDate.getNanoTime();
			int writeLen = 0;
			for(int i=0; i<WRITE_SIZE; i+=content.getBytes().length) {
				writeLen += content.getBytes().length;
				byteContent.put(content.getBytes());
				if(writeLen>=8192) {
					writeLen = 0;
					os.write(byteContent.array(), 0, byteContent.position());
					byteContent.clear();
				}
			}
			
			if(writeLen>0) {
				writeLen = 0;
				byteContent.put(content.getBytes());
				os.write(byteContent.array(), 0, byteContent.position());
				byteContent.clear();
			}
			os.flush();	
			os.close();
			long end = RDate.getNanoTime();
			System.out.println("writeByOutputStreamOptimization写入文件" + createF.getFileName() +"总耗时:" + (end - start) + "(纳秒)");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @author raohj
	 * @date 2019年3月28日
	 * @comment 使用缓冲区写入文件BufferedWriter
	 * @param 
	 * @return 
	 */
	public static void writebyBufferedWriter() {
		try {
			// 写入文件 BufferedWriter
			Integer d0 = rd.nextInt();
			Path createF = Paths.get("D:", "temp", "tmp", d0.toString());
			BufferedWriter bufferWriter = Files.newBufferedWriter(createF, StandardOpenOption.CREATE);
			long start = RDate.getNanoTime();
			for(int i=0; i<WRITE_SIZE; i+=content.getBytes().length) {
				bufferWriter.write(content);// 写入UTF-8信息
			}
			bufferWriter.flush();	// 刷新到磁盘
			bufferWriter.close();
			long end = RDate.getNanoTime();
			System.out.println("BufferedWriter写入文件" + createF.getFileName() +"总耗时:" + (end - start) + "(纳秒)");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @author raohj
	 * @date 2019年3月28日
	 * @comment 通过通道来写文件
	 * @param 
	 * @return 
	 */
	public static void writeByByteChannel() {
		try {
			// 写入文件 BufferedWriter
			Integer d0 = rd.nextInt();
			Path createF = Paths.get("D:", "temp", "tmp", d0.toString());
			WritableByteChannel wbc = Files.newByteChannel(createF, EnumSet.of(StandardOpenOption.CREATE,StandardOpenOption.WRITE));
			long start = RDate.getNanoTime();
			for(int i=0; i<WRITE_SIZE; i+=content.getBytes().length) {
				wbc.write(ByteBuffer.wrap(content.getBytes()));
			}
			wbc.close();
			long end = RDate.getNanoTime();
			System.out.println("ByteChannel写入文件" + createF.getFileName() +"总耗时:" + (end - start) + "(纳秒)");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
